#!/usr/bin/python

from socket import *

class commLector:
      def __init__(self, addr):
          self.addr = addr 
          try:
             self.s = socket(AF_INET, SOCK_STREAM)
             self.s.connect(self.addr)
          except Exception, str:
             raise Exception, "No hem puc connectar al servidor de lectures!"
             return

      def connectar(self):
          retval = True
          if(self.s.sendto("#$",self.addr)):
             data = self.s.recv(2)
             if data!="$#":
                print "No s'ha iniciat la comunicacio!"
                retval = False

          return retval 

      def enviar(self, cmd):
          retval = True
          datasnd = "$"+cmd+"$"
   	  if not (self.s.sendto(datasnd,self.addr)):
            retval = False 

          return retval

      def rebre(self):
          datarcv=''
          buff=''
          while datarcv != '\r':
                datarcv = self.s.recv(1)
                buff+=datarcv

          return buff 


